home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-08-10 | 5.8 KB | 202 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CSimpleUDPServer.cp ©1995-1998 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- // Servers are endpoints that watch and wait for connection requests from
- // remote clients. The actual connection to and interaction with remote clients
- // is handled by the "responders."
-
- #include "CSimpleUDPServer.h"
-
- #include <UNetworkFactory.h>
- #include <LWindow.h>
- #include <LApplication.h>
- #include <UMemoryMgr.h>
-
-
- // ===========================================================================
- // • Resource IDs
- // ===========================================================================
-
- const PP_PowerPlant::ResIDT PPob_UDPTerminalWindow = 130;
- const PP_PowerPlant::PaneIDT pPort = 'PORT';
- const PP_PowerPlant::PaneIDT pAddress = 'ADDR';
- const PP_PowerPlant::PaneIDT pTerminal = 'TERM';
-
- // ---------------------------------------------------------------------------
- // • CSimpleUDPServer
- // ---------------------------------------------------------------------------
-
- CSimpleUDPServer::CSimpleUDPServer(
- LCommander* inSuper)
- : LSingleDoc(inSuper)
- {
- mWindow = nil;
- mEndpoint = nil;
- mServerThread = nil;
- mQuitWhenDone = false;
- mSaveOption = 0;
- }
-
- // ---------------------------------------------------------------------------
- // • ~CSimpleUDPServer
- // ---------------------------------------------------------------------------
-
- CSimpleUDPServer::~CSimpleUDPServer()
- {
- if (mEndpoint) {
- delete mEndpoint;
- mEndpoint = nil;
- }
-
- if (mServerThread)
- mServerThread->DeleteThread();
- }
-
- // ---------------------------------------------------------------------------
- // • WaitForUDPData
- // ---------------------------------------------------------------------------
- // Wait for connection(s) from remote machine(s) using the parameters that have
- // been passed to us. The connection information window is also opened at this time.
- //
- // This routine creates an endpoint and a thread for the server process.
- // When the thread runs, it will bind the endpoint and wait for T_Listen events.
- // Responses to T_Listens will be handled on NEW endpoints. This endpoint ONLY listens.
-
- void
- CSimpleUDPServer::WaitForUDPData(
- UInt32 inPort)
- {
- CreateServerWindow(inPort);
-
- mEndpoint = PP_PowerPlant::UNetworkFactory::CreateUDPEndpoint();
-
- mServerThread = new CUDPServerThread(
- inPort,
- mEndpoint,
- this,
- mTerminalPane);
-
- mServerThread->Resume();
- }
-
- // ---------------------------------------------------------------------------
- // • ServerThreadDied
- // ---------------------------------------------------------------------------
-
- void
- CSimpleUDPServer::ServerThreadDied()
- {
- mServerThread = nil;
-
- //Re-issue the quit command when we are ready.
- if (mQuitWhenDone) {
- PP_PowerPlant::LApplication* theApp = dynamic_cast<PP_PowerPlant::LApplication*>(GetSuperCommander());
- if ( theApp ) {
- theApp->DoQuit(mSaveOption);
- }
- }
-
- delete this;
- }
-
- PP_PowerPlant::LUDPEndpoint*
- CSimpleUDPServer::GetEndPoint() const
- {
- return mEndpoint;
- }
-
- // ---------------------------------------------------------------------------
- // • CreateServerWindow
- // ---------------------------------------------------------------------------
-
- void
- CSimpleUDPServer::CreateServerWindow(
- UInt32 inPort)
- {
- PP_PowerPlant::LCaption* theField;
-
- mWindow = PP_PowerPlant::LWindow::CreateWindow(PPob_UDPTerminalWindow, this);
-
- mTerminalPane = dynamic_cast<CTerminalPane*> (mWindow->FindPaneByID(pTerminal));
- ThrowIfNil_ (mTerminalPane);
-
- theField = dynamic_cast<PP_PowerPlant::LCaption*> (mWindow->FindPaneByID(pPort));
- ThrowIfNil_ (theField);
- theField->SetValue(inPort);
-
- theField = dynamic_cast<PP_PowerPlant::LCaption*> (mWindow->FindPaneByID(pAddress));
- ThrowIfNil_ (theField);
-
- PP_PowerPlant::StDeleter<PP_PowerPlant::LInternetMapper>
- theMapper(PP_PowerPlant::UNetworkFactory::CreateInternetMapper());
-
- PP_PowerPlant::StDeleter<PP_PowerPlant::LInternetAddress>
- tempAddress(theMapper->GetLocalAddress());
-
- Str255 tempDescriptor;
- theField->SetDescriptor(tempAddress->GetIPDescriptor(tempDescriptor));
- }
-
- // ---------------------------------------------------------------------------
- // • BindCompleted
- // ---------------------------------------------------------------------------
- // We are now waiting for T_Listens... show the connection window
-
- void
- CSimpleUDPServer::BindCompleted()
- {
- mWindow->Show();
- }
-
- // ---------------------------------------------------------------------------
- // • BindFailed
- // ---------------------------------------------------------------------------
- // Could not bind to the requested port.
-
- void
- CSimpleUDPServer::BindFailed()
- {
- SignalPStr_("\pBind Failed");
- delete this;
- }
-
- // ---------------------------------------------------------------------------
- // • AllowSubRemoval
- // ---------------------------------------------------------------------------
- // If user tries to close the main window, and we have ongoing connections,
- // then block the action. If we don't have any connections then
- // interpret this as a request to close the server.
-
- Boolean
- CSimpleUDPServer::AllowSubRemoval(
- LCommander *inSub)
- {
- if (inSub == mWindow) {
- mServerThread->Complete();
- return false;
- }
- else
- return LSingleDoc::AllowSubRemoval(inSub);
- }
-
- // ---------------------------------------------------------------------------
- // • AttemptQuitSelf
- // ---------------------------------------------------------------------------
- // Close our connection before quitting. We re-issue the quit command via
- // the ServerThreadDied method once we are done.
-
- Boolean
- CSimpleUDPServer::AttemptQuitSelf(
- SInt32 inSaveOption)
- {
- if (mQuitWhenDone) {
- return LCommander::AttemptQuitSelf(inSaveOption);
- } else {
- mQuitWhenDone = true;
- mSaveOption = inSaveOption;
-
- mServerThread->Complete();
- return false;
- }
- }
-